package com.nt.oam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nt.common.core.domain.entity.SysRole;
import com.nt.common.core.domain.entity.SysUser;
import com.nt.common.enums.YesOrNoEnum;
import com.nt.common.utils.DateUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.nt.common.utils.SecurityUtils;
import com.nt.common.utils.StringUtils;
import com.nt.common.utils.bean.BeanUtils;
import com.nt.oam.domain.*;
import com.nt.oam.vo.asset.AccessDeviceAssetVo;
import com.nt.system.service.impl.SysRoleServiceImpl;
import com.nt.system.service.impl.SysUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nt.oam.mapper.AntAccessDeviceAssetMapper;
import com.nt.oam.service.IAntAccessDeviceAssetService;

/**
 * 接入设备资产Service业务层处理
 *
 * @author JZFeng
 * @date 2025-02-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AntAccessDeviceAssetServiceImpl extends ServiceImpl<AntAccessDeviceAssetMapper, AntAccessDeviceAsset> implements IAntAccessDeviceAssetService {
    private final AntAccessDeviceAssetMapper antAccessDeviceAssetMapper;
    private final SysRoleServiceImpl sysRoleService;
    private final AntHospitalServiceImpl antHospitalService;
    private final AntHospitalProjectServiceImpl antHospitalProjectService;
    private final AntAssetCorrelationServiceImpl antAssetCorrelationService;
    private final SysUserServiceImpl sysUserService;

    /**
     * 查询接入设备资产
     *
     * @param accessDeviceId 接入设备资产主键
     * @return 接入设备资产
     */
    @Override
    public AntAccessDeviceAsset selectAntAccessDeviceAssetByAccessDeviceId(Long accessDeviceId) {
        return antAccessDeviceAssetMapper.selectAntAccessDeviceAssetByAccessDeviceId(accessDeviceId);
    }

    /**
     * 查询接入设备资产列表
     *
     * @param antAccessDeviceAsset 接入设备资产
     * @return 接入设备资产
     */
    @Override
    public List<AntAccessDeviceAsset> selectAntAccessDeviceAssetList(AntAccessDeviceAsset antAccessDeviceAsset) {
        List<AntAccessDeviceAsset> antAccessDeviceAssetList = antAccessDeviceAssetMapper.selectAntAccessDeviceAssetList(antAccessDeviceAsset);

        return antAccessDeviceAssetList;
    }

    /**
     * 新增接入设备资产
     *
     * @param antAccessDeviceAsset 接入设备资产
     * @return 结果
     */
    @Override
    public int insertAntAccessDeviceAsset(AntAccessDeviceAsset antAccessDeviceAsset) {
        antAccessDeviceAsset.setCreateTime(DateUtils.getNowDate());
        // 医院的id
        // 医院的id
        Long hospitalId = antAccessDeviceAsset.getHospitalId();
        antAccessDeviceAsset.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antAccessDeviceAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antAccessDeviceAsset.getProjectId();
        antAccessDeviceAsset.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antAccessDeviceAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antAccessDeviceAsset.setCreatePerson(sysUser.getUserName());
        }
        // 是否删除，默认否
        antAccessDeviceAsset.setIsDeleted(YesOrNoEnum.NO.getCode());
        antAccessDeviceAsset.setIsModify(YesOrNoEnum.NO.getCode());
        return antAccessDeviceAssetMapper.insertAntAccessDeviceAsset(antAccessDeviceAsset);
    }

    /**
     * 修改接入设备资产
     *
     * @param antAccessDeviceAsset 接入设备资产
     * @return 结果
     */
    @Override
    public int updateAntAccessDeviceAsset(AntAccessDeviceAsset antAccessDeviceAsset) {
        antAccessDeviceAsset.setUpdateTime(DateUtils.getNowDate());
        // 医院的id
        // 医院的id
        Long hospitalId = antAccessDeviceAsset.getHospitalId();
        antAccessDeviceAsset.setHospitalId(hospitalId);
        // 查询医院的名称
        AntHospital antHospital = antHospitalService.getById(hospitalId);
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antAccessDeviceAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目id
        Long projectId = antAccessDeviceAsset.getProjectId();
        antAccessDeviceAsset.setProjectId(projectId);
        AntHospitalProject antHospitalProject = antHospitalProjectService.getById(projectId);
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antAccessDeviceAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 更新人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antAccessDeviceAsset.setUpdatePerson(sysUser.getUserName());
        }
        return antAccessDeviceAssetMapper.updateAntAccessDeviceAsset(antAccessDeviceAsset);
    }

    /**
     * 批量删除接入设备资产
     *
     * @param accessDeviceIds 需要删除的接入设备资产主键
     * @return 结果
     */
    @Override
    public int deleteAntAccessDeviceAssetByAccessDeviceIds(Long[] accessDeviceIds) {
        List<Long> accessDeviceIdsList = Arrays.asList(accessDeviceIds);
        // 状态删除
        accessDeviceIdsList.forEach(a -> {
            AntAccessDeviceAsset antAccessDeviceAsset = antAccessDeviceAssetMapper.selectAntAccessDeviceAssetByAccessDeviceId(a);
            antAccessDeviceAsset.setIsDeleted(YesOrNoEnum.YES.getCode());
            antAccessDeviceAssetMapper.updateAntAccessDeviceAsset(antAccessDeviceAsset);
        });
        //antAccessDeviceAssetMapper.deleteAntAccessDeviceAssetByAccessDeviceIds(accessDeviceIds);

        return YesOrNoEnum.YES.getCode();
    }

    /**
     * 删除接入设备资产信息
     *
     * @param accessDeviceId 接入设备资产主键
     * @return 结果
     */
    @Override
    public int deleteAntAccessDeviceAssetByAccessDeviceId(Long accessDeviceId) {
        return antAccessDeviceAssetMapper.deleteAntAccessDeviceAssetByAccessDeviceId(accessDeviceId);
    }

    /**
     * 非系统----查询接入设备资产
     *
     * @param accessDeviceId 接入设备资产主键
     * @return 接入设备资产
     */
    @Override
    public AccessDeviceAssetVo selectAccessDeviceAssetMessage(Long accessDeviceId) {
        // 源对象
        AntAccessDeviceAsset antAccessDeviceAsset = antAccessDeviceAssetMapper.selectAntAccessDeviceAssetByAccessDeviceId(accessDeviceId);
        //医院名称
        AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antAccessDeviceAsset.getHospitalId());
        if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
            antAccessDeviceAsset.setHospitalName(antHospital.getHospitalName());
        }
        // 项目名称
        AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antAccessDeviceAsset.getProjectId());
        if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
            antAccessDeviceAsset.setProjectName(antHospitalProject.getProjectName());
        }
        // 资产名称
        AntAssetCorrelation antAssetCorrelation = antAssetCorrelationService.selectAntAssetCorrelationByAssetId(antAccessDeviceAsset.getAssetId());
        if (Objects.nonNull(antAssetCorrelation) && StringUtils.isNotEmpty(antAssetCorrelation.getAssetName())) {
            antAccessDeviceAsset.setAssetName(antAssetCorrelation.getAssetName());
        }
        // 目标对象
        AccessDeviceAssetVo accessDeviceAssetVo = new AccessDeviceAssetVo();
        // 赋值目标对象
        BeanUtils.copyProperties(antAccessDeviceAsset, accessDeviceAssetVo);
        return accessDeviceAssetVo;
    }

    /**
     * 通过项目id来查询接入设备资产
     *
     * @return 接入设备资产
     */
    @Override
    public List<AccessDeviceAssetVo> selectByProjectIdAccessDeviceAsset(Long projectId) {
        List<AccessDeviceAssetVo> accessDeviceAssetVoList = new ArrayList<>();
        // 源对象
        List<AntAccessDeviceAsset> antAccessDeviceAssetList = antAccessDeviceAssetMapper.selectByProjectIdAccessDeviceAsset(projectId);
        if (Objects.nonNull(antAccessDeviceAssetList) && antAccessDeviceAssetList.size() > 0) {
            antAccessDeviceAssetList.forEach(antAccessDeviceAsset -> {
                //医院名称
                AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antAccessDeviceAsset.getHospitalId());
                if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                    antAccessDeviceAsset.setHospitalName(antHospital.getHospitalName());
                }
                // 项目名称
                AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antAccessDeviceAsset.getProjectId());
                if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                    antAccessDeviceAsset.setProjectName(antHospitalProject.getProjectName());
                }
                // 资产名称
                AntAssetCorrelation antAssetCorrelation = antAssetCorrelationService.selectAntAssetCorrelationByAssetId(antAccessDeviceAsset.getAssetId());
                if (Objects.nonNull(antAssetCorrelation) && StringUtils.isNotEmpty(antAssetCorrelation.getAssetName())) {
                    antAccessDeviceAsset.setAssetName(antAssetCorrelation.getAssetName());
                }
                // 目标对象
                AccessDeviceAssetVo accessDeviceAssetVo = new AccessDeviceAssetVo();
                // 赋值目标对象
                BeanUtils.copyProperties(antAccessDeviceAsset, accessDeviceAssetVo);
                accessDeviceAssetVoList.add(accessDeviceAssetVo);
            });
        }
        return accessDeviceAssetVoList;
    }


    /**
     * 通过医院id来查询接入设备资产
     *
     * @return 接入设备资产
     */
    @Override
    public List<AccessDeviceAssetVo> selectByHospitalIdAccessDeviceAsset(Long hospitalId) {
        List<AccessDeviceAssetVo> accessDeviceAssetVoList = new ArrayList<>();
        // 源对象
        List<AntAccessDeviceAsset> antAccessDeviceAssetList = antAccessDeviceAssetMapper.selectByHospitalIdAccessDeviceAsset(hospitalId);
        if (Objects.nonNull(antAccessDeviceAssetList) && antAccessDeviceAssetList.size() > 0) {
            antAccessDeviceAssetList.forEach(antAccessDeviceAsset -> {
                //医院名称
                AntHospital antHospital = antHospitalService.selectAntHospitalByHospitalId(antAccessDeviceAsset.getHospitalId());
                if (Objects.nonNull(antHospital) && StringUtils.isNotEmpty(antHospital.getHospitalName())) {
                    antAccessDeviceAsset.setHospitalName(antHospital.getHospitalName());
                }
                // 项目名称
                AntHospitalProject antHospitalProject = antHospitalProjectService.selectAntHospitalProjectByProjectId(antAccessDeviceAsset.getProjectId());
                if (Objects.nonNull(antHospitalProject) && StringUtils.isNotEmpty(antHospitalProject.getProjectName())) {
                    antAccessDeviceAsset.setProjectName(antHospitalProject.getProjectName());
                }
                // 资产名称
                AntAssetCorrelation antAssetCorrelation = antAssetCorrelationService.selectAntAssetCorrelationByAssetId(antAccessDeviceAsset.getAssetId());
                if (Objects.nonNull(antAssetCorrelation) && StringUtils.isNotEmpty(antAssetCorrelation.getAssetName())) {
                    antAccessDeviceAsset.setAssetName(antAssetCorrelation.getAssetName());
                }
                // 目标对象
                AccessDeviceAssetVo accessDeviceAssetVo = new AccessDeviceAssetVo();
                // 赋值目标对象
                BeanUtils.copyProperties(antAccessDeviceAsset, accessDeviceAssetVo);
                accessDeviceAssetVoList.add(accessDeviceAssetVo);
            });
        }
        return accessDeviceAssetVoList;
    }

    /**
     * 查询是否在修改
     */
    public int selectDeviceModify(Long accessDeviceId) {
        AntAccessDeviceAsset antAccessDeviceAsset = this.getById(accessDeviceId);
        int isModify = 0;
        if (Objects.nonNull(antAccessDeviceAsset) && Objects.nonNull(antAccessDeviceAsset.getIsModify())) {
            isModify = antAccessDeviceAsset.getIsModify();
        }
        return isModify;
    }

    /**
     * 重新设置修改状态
     */
    public int updateDeviceModify(Long accessDeviceId, Integer isModify) {
        AntAccessDeviceAsset antAccessDeviceAsset = this.getById(accessDeviceId);
        if (Objects.nonNull(antAccessDeviceAsset)) {
            antAccessDeviceAsset.setIsModify(isModify);
        }
        return antAccessDeviceAssetMapper.updateAntAccessDeviceAsset(antAccessDeviceAsset);
    }
}
